<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 放置内容 -->
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="height:60px">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <el-table border :data="list">
              <el-table-column
                align="center"
                label="序号"
                width="120px"
                type="index"
              ></el-table-column>
              <el-table-column
                align="center"
                label="角色名"
                width="240px"
                prop="name"
              ></el-table-column>
              <el-table-column
                align="center"
                label="描述"
                prop="description"
              ></el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="deleteRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 放置分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height:60px"
            >
              <el-pagination
                layout="prev, pager, next"
                :total="page.total"
                :page-sizes="[1, 2, 4, 5]"
                :page-size="page.pagesize"
                :current-page="page.page"
                @current-change="changePage"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <!-- 右边信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            >
            </el-alert>
            <!-- 并不是表单都要绑定:model :rules 校验时才需要 -->
            <el-form
              :model="formData"
              label-width="120px"
              style="margin-top:50px"
            >
              <el-form-item label="企业名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width:400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width:400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input
                  v-model="formData.companyPhone"
                  disabled
                  style="width:400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="公司邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width:400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="公司备注">
                <el-input
                  v-model="formData.remarks"
                  disabled
                  style="width:400px"
                  type="textarea"
                  :row="3"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 编辑对话框 -->
    <el-dialog
      title="编辑部门"
      :visible="showDialog"
      width="40%"
      @close="btnCel"
    >
      <el-form
        ref="roleRef"
        :model="roleForm"
        :rules="rules"
        label-width="90px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description"> </el-input>
        </el-form-item>
      </el-form>
      <!-- 放置footer插槽 -->
      <el-row type="flex" justify="center">
        <el-col :span="20">
          <el-button size="small" @click="btnCel"> 取消 </el-button>
          <el-button size="small" type="primary" @click="btnOK">
            确定
          </el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限对话框 -->
    <el-dialog
      :visible="showPermDialog"
      title="分配权限"
      @close="btnPermCancel"
    >
      <!-- 权限是树形结构 -->

      <el-tree
        ref="perTree"
        :data="permData"
        :props="defaultProps"
        :show-checkbox="true"
        :check-strictly="true"
        :default-expand-all="true"
        :default-checked-keys="selectCheck"
        node-key="id"
      />
      <!-- 确定 取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK"
            >确定</el-button
          >
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  updateRole,
  getRoleDetail,
  addRole,
  assignPerm
} from '@/api/setting.js'
import {
  updatePermission,
  addPermission,
  getPermissionDetail,
  delPermission,
  getPermissionList
} from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      list: [], //承接数组
      page: {
        // 放置页码及相关数据
        page: 1, //当前页码
        pagesize: 5, //每页条数
        total: 0 //总数据条数
      },
      // 公司表单数据
      formData: {},
      // 控制编辑对话框的显示
      showDialog: false,
      // 控制分配权限对话框的显示
      showPermDialog: false,
      // 角色表单数据
      roleForm: {
        name: '',
        description: ''
      },
      // 编辑表单校验规则
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      // 接受权限数据
      permData: [],
      // 定义显示字段的名称和子属性的字段
      defaultProps: {
        label: 'name'
      },
      // 用来记录当前分配权限的id
      roleId: null,
      // 用来记录当前权限点的标识
      selectCheck: []
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 监听页码值的变化
    changePage(newPage) {
      // newPage当前点击页码
      this.page.page = newPage
      this.getRoleList()
    },
    deleteRole(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 确认删除 返回Promise对象
          return deleteRole(id)
        })
        .then(() => {
          // 删除成功进入这里
          this.getRoleList() // 触发自定义事件
          this.$message.success('删除角色成功')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 弹出编辑对话框
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)
      // 为了不出现闪烁的问题 先获取数据 再显示对话框
      this.showDialog = true
    },
    async btnOK() {
      try {
        await this.$refs.roleRef.validate()
        // roleForm 有id就是编辑   没有就是新增
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        //  重新更新数据
        this.getRoleList()
        this.$message.success('操作成功!')
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCel() {
      this.$refs.roleRef.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
      this.showDialog = false
    },
    // 弹出分配权限对话框
    async assignPerm(id) {
      //获取所有权限点 得到树形结构
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      this.roleId = id
      // 获取这个id的权限
      const { permIds } = await getRoleDetail(id)
      // 将当前角色所拥有的权限id赋值
      this.selectCheck = permIds
      this.showPermDialog = true
    },
    async btnPermOK() {
      // 调用el-tree 中的getCheckedKeys方法
      await assignPerm({
        permIds: this.$refs.perTree.getCheckedKeys(),
        id: this.roleId
      })
      this.$message.success('分配权限成功！')
      this.showPermDialog = false
    },
    btnPermCancel() {
      // 重置数据  复选框的问题
      this.selectCheck = []
      this.showPermDialog = false
    }
  }
}
</script>
<style scoped></style>
